﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace Trade.Controls
{
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    public class RayTextBox: TextBox
    {
        private bool changeForeColor = false;

        public RayTextBox(): base()
        {
            this.Font = new Font(this.Font.Name, 10, FontStyle.Bold);
            //this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

        [Description("Enabled == false时是否将ForeColor设为Black")]
        public bool ChangeForeColor
        {
            set { changeForeColor = value; }
            get { return changeForeColor; }
        }

        //改变TextBox的Enabled==false时的Text的ForeColor须重写OnEnabledChanged及OnPaint
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);

            if (changeForeColor == true)
            {
                SetStyle(ControlStyles.UserPaint, Enabled == false ? true : false);
                Invalidate();
                RecreateHandle();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            string text = this.Text;

            if (changeForeColor == true)
            {
                Pen pen = new Pen(Color.Black, 1);
                Rectangle rect = new Rectangle(-1, 1, e.ClipRectangle.Width, e.ClipRectangle.Height);
                TextRenderer.DrawText(e.Graphics, text, this.Font, rect, Color.Black, TextFormatFlags.Left);
                e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1));
            }
        }
    }
}
